package com.dark;

import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.data.redis.core.BoundValueOperations;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.SessionCallback;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.AbstractJUnit4SpringContextTests;

import java.io.Serializable;
import java.util.concurrent.TimeUnit;

/**
 * Created by tengxue on 16-6-21.
 */
@ContextConfiguration(locations = {"classpath*:applicationContext.xml"})
public class ObjectRedisDemo extends AbstractJUnit4SpringContextTests {

    private static final String  PERSON_NAME="person";

    @Autowired
    private RedisTemplate<String,Person> objectRedisTemplate;

    static class Person implements Serializable{
        private String  firstName;
        private String  lastName;
        private Integer age;
        private String  sex;

        public Person(String firstName,String lastName,Integer age,String sex){
            this.firstName=firstName;
            this.lastName=lastName;
            this.age=age;
            this.sex=sex;
        }

        public String getFirstName() {
            return firstName;
        }

        public void setFirstName(String firstName) {
            this.firstName = firstName;
        }

        public String getLastName() {
            return lastName;
        }

        public void setLastName(String lastName) {
            this.lastName = lastName;
        }

        public Integer getAge() {
            return age;
        }

        public void setAge(Integer age) {
            this.age = age;
        }

        public String getSex() {
            return sex;
        }

        public void setSex(String sex) {
            this.sex = sex;
        }

        @Override
        public String toString() {
            return "Person{" +
                    "firstName='" + firstName + '\'' +
                    ", lastName='" + lastName + '\'' +
                    ", age=" + age +
                    ", sex='" + sex + '\'' +
                    '}';
        }
    }

    @Test
    public void testAddObject() {
        Person dark=new Person("xue","dark",30,"male");
        objectRedisTemplate.opsForValue().set(PERSON_NAME,dark);
        objectRedisTemplate.expire(PERSON_NAME,5, TimeUnit.MINUTES);
    }

    @Test
    public void testGetObject() {
        Person dark=objectRedisTemplate.opsForValue().get(PERSON_NAME);
        System.out.println("the dark is :" + dark);

    }

    @Test
    public void testTransaction() {
        Person dark=new Person("xue","dark",30,"male");
        objectRedisTemplate.opsForValue().set(PERSON_NAME,dark);
        objectRedisTemplate.expire(PERSON_NAME,5, TimeUnit.MINUTES);

        SessionCallback<Person> sessionCallback = new SessionCallback<Person>() {
            public Person execute(RedisOperations operations) throws DataAccessException {
                operations.multi();
                Person dark=new Person("xue","dark",25,"male");
                BoundValueOperations<String, Person> oper = operations.boundValueOps(PERSON_NAME);
                oper.set(dark);
                oper.expire(5, TimeUnit.MINUTES);
                int x=1/0;
                operations.exec();
                return dark;
            }
        };
        objectRedisTemplate.execute(sessionCallback);

    }
}
